<?php
/**
 * Created by PhpStorm.
 * User: xpz
 * Date: 2025/3/20
 * Time: 13:32
 */

namespace app\api\command;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Exception;
use think\config;
use think\Db;
use WebSocket\Client;
use think\cache;

class Images extends Command
{
    private $rootPath;
    protected function configure()
    {
        $this->setName('images')              		//定义命令的名字
        ->setDescription('images')      //定义命令的描述
        ->addArgument('method')                   //参数1
        ->addArgument('path');                    //文章id
    }

    //php think image mv_image_name(publish)
    protected function execute(Input $input, Output $output)
    {
        $this->rootPath = ROOT_PATH."public";
        ini_set('max_execution_time', 600);//秒为单位，自己根据需要定义
//        require EXTEND_PATH.'/websocket/lib/Client.php';
        $method = $input->getArgument('method');
        if($method == 'crop_image'){
            $img = $input->getArgument('path');
            $this->cropImage($img);
            $this->cropImage($img,'bigcompress','770');
        } else if($method == 'mv_image_name'){
            $this->_mvImage();
        } else if($method == 'init'){
            $this->init();
        }
    }

    private function cropImage($imageUrl,$defaultImage='compress',$size='320'){
//        $imageUrl = "/uploads/20250319/172474f347efa4b72e3ba62986b9cfc4.jpg";
        $inputImage = $this->rootPath.$imageUrl;
        $outputImage = $this->rootPath."/".$defaultImage.$imageUrl;
        $fileArr = pathinfo($imageUrl);
//        print_r($fileArr);
        $fileDir = $this->rootPath."/".$defaultImage.$fileArr['dirname'];
//        echo $fileDir;exit;
        if (!is_dir($fileDir)) {
            mkdirs($fileDir);
        }
        $shell = '/soft/ffmpeg-7.0.2-amd64-static/ffmpeg -y -i '.$inputImage.' -vf scale='.$size.':-1 '.$outputImage;
        exec($shell, $output, $ret);
    }



    private function init(){
        $recordModel = Model('app\common\model\Article');
        $recordData = $recordModel->select();
        if(!empty($recordData)){
            foreach($recordData as $k=>$v){
                $this->mvImageName($v['id']);
//                $this->cropImage($v['oriimage']);
//                $this->cropImage($v['oriimage'],'bigcompress','770');
            }
        }
    }

    private function _mvImage(){
        $mvmodel = model('app\common\model\Mvimage');
        $articleData = $mvmodel->select();
        if(!empty($articleData)){
            foreach($articleData as $k=>$v){
                $this->mvImageName($v['aid']);
                $mvmodel->where(['id'=>$v['id']])->delete();
            }
        }
    }
    private function mvImageName($id){
//        $id = 7;
        $recordModel = Model('app\common\model\Article');
        $recordData = $recordModel->where(['id'=>$id])->find();
//        print_r($recordData);exit;

        $source = $recordData['ori_image'];
        // 获取文件名（带扩展名）
        $filename = basename($source);

        // 分离文件名和扩展名
        $hash = pathinfo($filename, PATHINFO_FILENAME);
        $destination = str_replace($hash,$recordData['url_path'],$source);
//        echo $destination;exit;
        //修改数据库里的名字
        $updateRs = $recordModel->where(['id'=>$id])->update(['image'=>$destination]);
        if($updateRs == 1){
            //修改compress里的图片
            $compressSource = $this->rootPath."/compress".$source;
            $compressDestination = $this->rootPath."/compress".$destination;
            if (rename($compressSource, $compressDestination)) {
                echo $id."=>compress文件移动成功！";
            } else {
                echo $id."=>compress文件移动失败！";
            }
            $compressBigSource = $this->rootPath."/bigcompress".$source;
            $compressBigDestination = $this->rootPath."/bigcompress".$destination;
            if (rename($compressBigSource, $compressBigDestination)) {
                echo $id."=>bigcompress！";
            } else {
                echo $id."=>bigcompress！";
            }
            Cache::rm('article_details_cache_'.$recordData['url_path']);
        } else {
            echo $id."=>数据库修改失败！";
        }
//        echo $hash;exit;


//        /compress/uploads/20250313/72936fbd83aeb80c32b1a7017520f2f7.webp

    }
}